package com.emep.changzhi.analyse.http.error;

import com.emep.changzhi.analyse.entity.error.RoadDust;
import com.emep.changzhi.analyse.model.Result;
import com.emep.changzhi.analyse.model.ResultBean;
import com.emep.changzhi.analyse.service.error.RoadDustService;
import com.emep.changzhi.analyse.utils.Constant;
import com.emep.changzhi.analyse.utils.DataValidator;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author xy
 * @Description: 道路扬尘监测异常控制器
 * @date 2018/2/27 9:21
 * @version V1.0
 */
@RestController
@RequestMapping("roadDust")
@Api(value="道路扬尘监测异常controller",tags={"道路扬尘监测异常操作接口"})
public class RoadDustController {
    @Autowired
    private RoadDustService roadDustService;



    /**
     * 新增道路扬尘监测异常数据
     *
     * @param payload
     * @return
     */
    @ApiOperation(value = "新增道路扬尘监测异常数据", notes = "新增道路扬尘监测异常数据")
    @ApiResponses({@ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "修改时传的id", required = false, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "mn", value = "设备mn号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "dataType", value = "数据类型", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "polluteType", value = "污染类型", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "areaCode", value = "行政区划code", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "content", value = "报文内容", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "contentDate", value = "报文时间", required = true, dataType = "String", paramType = "query"),

    })
    @PostMapping(value = "add")
    public ResultBean<RoadDust> add(@RequestParam Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "mn", "dataType", "polluteType",
                "areaCode", "content", "contentDate");
        if (result.getCode() == 1) {
           return result.boolAdd(roadDustService.save(payload));
        }
        return result;
    }
    /**
     * @author xy
     * @Description: 首页
     * @date 2018/3/8 11:49
     * @version V1.0
     */
    @ApiOperation(value="首页", notes="首页")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "查询数量", required = true, dataType = "String",paramType="query")
    })
    @GetMapping(value = "home")
    public ResultBean<List<RoadDust>> findRoadDustByHome(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "pageSize");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            return result.ok(roadDustService.findRoadDustByHome(Integer.parseInt(payload.get("pageSize").toString())));
        }
        return  result;
    }
    /**
     * @author xy
     * @Description: 翻页查询
     * @date 2018/3/8 11:49
     * @version V1.0
     */
    @ApiOperation(value="翻页查询", notes="翻页查询")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "查询起始id", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "pageSize", value = "查询数量", required = true, dataType = "Integer",paramType="query")
    })
    @GetMapping(value = "flip")
    public ResultBean<List<RoadDust>> findRoadDustByFlip(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id","pageSize");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            return result.ok(roadDustService.findRoadDustByFlip(payload.get("id").toString() , Integer.parseInt(payload.get("pageSize").toString())));
        }
        return result;
    }
}
